#include "LoaderAnim.h"

LoaderAnim::LoaderAnim(std::string Identifiant,int nbr):Loader(Identifiant)
{
    Nb_sprite=nbr;
    m_i=0;

    //ctor
}

LoaderAnim::~LoaderAnim()
{
    //dtor
}

sf::Sprite LoaderAnim::Get(unsigned int i) {
    if(((v_Sprite.size())>i)||(i<0)){
        return v_Sprite[i];
    }
    else{
        sf::Image im;
        im.LoadFromFile("data/error.png");
        sf::Sprite Spri;
        Spri.SetImage(im);
        std::cout<<std::endl<<"Erreur lors de la requete, indice out of boundaries";
        return Spri;
    }

}

sf::Sprite LoaderAnim::GetSprite(){
            if (m_i==Nb_sprite){
                m_i=0;
            }
            m_i=m_i+1;
            return v_Sprite[m_i];}

bool LoaderAnim::Load(){
    std::string temp="data/"+Id;
    if(!m_Image.LoadFromFile(temp)){
        std::cerr<<"Error during import "<<temp<<std::endl; // On affiche l'erreur
        return false;
    }
     m_Sprite.SetImage(m_Image);
    int L=m_Sprite.GetSize().x;
    int H=m_Sprite.GetSize().y;
    int L_subrect=L/Nb_sprite;

    for(int i=0;i<Nb_sprite;i++){
        sf::Sprite temp=m_Sprite;
        sf::IntRect S ((i*L_subrect),0, ((i+1)*L_subrect), H);
        temp.SetSubRect(S);
        v_Sprite.push_back(temp);
        std::cout<<std::endl<<"Load ..."<<i<<"..."<<v_Sprite.size();
    }

    return true;
}
